﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.ViewModels;
using Fayshine.VA.Helpers;

namespace Fayshine.VA.Views.SysConfig
{
    public partial class CameraSettings : Page
    {
        private ShopSettingViewModel m_ViewModel = null;
        private bool m_IsConfigUser = true;
        public CameraSettings()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(CameraSettings_Loaded);
        }

        void CameraSettings_Loaded(object sender, RoutedEventArgs e)
        {
            ValidUserRole();
            m_ViewModel = new ShopSettingViewModel();
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllCamerasIncludeGate();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void cmbShops_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbShops.SelectedIndex >= 0)
            {
                if (m_ViewModel != null)
                {
                    m_ViewModel.UpdateSelectArea((int)cmbShops.SelectedValue);
                }
            }
        }

        private void cmbDoors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbDoors.SelectedIndex >= 0 && cmbShops.SelectedIndex >= 0)
            {
                if(m_IsConfigUser) btn1.IsEnabled = true;
                if (m_IsConfigUser) btn2.IsEnabled = true;
                if (m_ViewModel != null)
                {
                    m_ViewModel.UpdateSelectGate((int)cmbDoors.SelectedValue);
                }
            }
            else
            {
                if (m_IsConfigUser) btn1.IsEnabled = false;
                if (m_IsConfigUser) btn2.IsEnabled = false;
            }
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            camera selectCamera = listEffectCameras.SelectedItem as camera;
            if (selectCamera != null)
            {
                selectCamera.AreaID = (int)cmbDoors.SelectedValue;
                if (m_ViewModel != null)
                {
                    m_ViewModel.AppendCamera(selectCamera);
                }
            }
        }

        private void btn2_Click(object sender, RoutedEventArgs e)
        {
            camera selectCamera = listSelectedCameras.SelectedItem as camera;
            if (selectCamera != null)
            {
                selectCamera.AreaID = null;
                if (m_ViewModel != null)
                {
                    m_ViewModel.RemoveCamera(selectCamera);
                }
            }
        }

        private void ValidUserRole()
        {
            user user = SessionManager.Session["UserInfo"] as user;
            if (user != null)
            {
                if (user.RoleID == 3)
                {
                    m_IsConfigUser = false;
                    this.btn1.IsEnabled = false;
                    this.btn2.IsEnabled = false;
                }
                else
                {
                    m_IsConfigUser = true;
                    this.btn1.IsEnabled = true;
                    this.btn2.IsEnabled = true;
                }
            }
        }

    }
}
